package space.xxhui.consumer.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author HitvzByHui
 * date 2021-09-28
 */
public class LocalDateUtils {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static LocalDateTime dateToLocalDateTime(Date date) {
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 获取秒数
     */
    public static long getDateTimeSecond(LocalDateTime dateTime) {
        return dateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 获取秒数
     */
    public static long getDateSecond(LocalDate date) {
        return date.atTime(0, 0).toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 获取毫秒数
     */
    public static long getDateTimeMilli(LocalDateTime dateTime) {
        return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 获取毫秒数
     */
    public static long getDateMilli(LocalDate date) {
        return date.atTime(0, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 日期转LocalDate
     */
    public static LocalDate getDateToToLocalDate(String date) {
        LocalDate localDate = LocalDate.parse(date, DATE_FORMATTER);
        return localDate;
    }

    public static String toDateTime(LocalDateTime dateTime) {
        return DATE_TIME_FORMATTER.format(dateTime);
    }

    public static String toTime(LocalTime time) {
        return TIME_FORMATTER.format(time);
    }

    public static String getYearFirstDay(int year) {
        return LocalDate.of(year, 1, 1).toString();
    }

    public static String getYearLastDay(int year) {
        LocalDate localDate = LocalDate.of(year, 1, 1);
        return localDate.minusDays(1L).toString();
    }

    public static String getMonthFirstDay(LocalDate date) {
        return LocalDate.of(date.getYear(), date.getMonth(), 1).toString();
    }

    public static String getMonthLastDay(LocalDate date) {
        LocalDate localDate = LocalDate.of(date.getYear(), date.getMonth(), 1).plusMonths(1L);
        return localDate.minusDays(1L).toString();
    }

    /**
     * 周日是第一天
     */
    public static String getWeekFirstDaySunday(LocalDate date) {
        return date.minusDays(date.getDayOfWeek().getValue()).toString();
    }

    /**
     * 周六是最后一天
     */
    public static String getWeekLastDaySaturday(LocalDate date) {
        return date.plusDays(7 - date.getDayOfWeek().getValue() - 1).toString();
    }

    /**
     * 周一第一天
     */
    public static String getWeekFirstDayMonday(LocalDate date) {
        return date.minusDays(date.getDayOfWeek().getValue() - 1).toString();
    }

    /**
     * 周日是最后一天
     */
    public static String getWeekLastDaySunday(LocalDate date) {
        return date.plusDays(7 - (date.getDayOfWeek().getValue() - 1) - 1).toString();
    }

    public static void main(String[] args) {
        LocalDate localDate = getDateToToLocalDate("2021-10-10");
        System.out.println();
    }

}
